﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Models;
using AutoR.Services;
using AutoR.Tasks;
using LiteDB;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;

namespace AutoR.ViewModels
{
	// Token: 0x02000063 RID: 99
	public class OnLineTaskViewModel : BindableBase, INavigationAware, INavigatedAware, INavigatingAware
	{
		// Token: 0x17000041 RID: 65
		// (get) Token: 0x0600016D RID: 365 RVA: 0x000434FA File Offset: 0x000416FA
		private INavigationService NavigationService { get; }

		// Token: 0x17000042 RID: 66
		// (get) Token: 0x0600016E RID: 366 RVA: 0x00043502 File Offset: 0x00041702
		private IPageDialogService PageDialogService { get; }

		// Token: 0x17000043 RID: 67
		// (get) Token: 0x0600016F RID: 367 RVA: 0x000408F6 File Offset: 0x0003EAF6
		public GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x17000044 RID: 68
		// (get) Token: 0x06000170 RID: 368 RVA: 0x0004350A File Offset: 0x0004170A
		// (set) Token: 0x06000171 RID: 369 RVA: 0x00043514 File Offset: 0x00041714
		public int Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Type>k__BackingField == value)
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x17000045 RID: 69
		// (get) Token: 0x06000172 RID: 370 RVA: 0x00043541 File Offset: 0x00041741
		// (set) Token: 0x06000173 RID: 371 RVA: 0x0004354C File Offset: 0x0004174C
		public List<OlTask> List
		{
			[CompilerGenerated]
			get
			{
				return this.<List>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<List>k__BackingField, value))
				{
					return;
				}
				this.<List>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.List);
			}
		}

		// Token: 0x17000046 RID: 70
		// (get) Token: 0x06000174 RID: 372 RVA: 0x0004357C File Offset: 0x0004177C
		// (set) Token: 0x06000175 RID: 373 RVA: 0x00043584 File Offset: 0x00041784
		public OlTask Select
		{
			[CompilerGenerated]
			get
			{
				return this.<Select>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<Select>k__BackingField, value))
				{
					return;
				}
				this.<Select>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Select);
			}
		}

		// Token: 0x17000047 RID: 71
		// (get) Token: 0x06000176 RID: 374 RVA: 0x000435B4 File Offset: 0x000417B4
		// (set) Token: 0x06000177 RID: 375 RVA: 0x000435BC File Offset: 0x000417BC
		public List<TaskComment> Operation
		{
			[CompilerGenerated]
			get
			{
				return this.<Operation>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<Operation>k__BackingField, value))
				{
					return;
				}
				this.<Operation>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Operation);
			}
		}

		// Token: 0x17000048 RID: 72
		// (get) Token: 0x06000178 RID: 376 RVA: 0x000435EC File Offset: 0x000417EC
		public DelegateCommand OKCommand { get; }

		// Token: 0x17000049 RID: 73
		// (get) Token: 0x06000179 RID: 377 RVA: 0x000435F4 File Offset: 0x000417F4
		public DelegateCommand<OlTask> DingCommand { get; }

		// Token: 0x1700004A RID: 74
		// (get) Token: 0x0600017A RID: 378 RVA: 0x000435FC File Offset: 0x000417FC
		public DelegateCommand<OlTask> TreadCommand { get; }

		// Token: 0x1700004B RID: 75
		// (get) Token: 0x0600017B RID: 379 RVA: 0x00043604 File Offset: 0x00041804
		public DelegateCommand<OlTask> DelCommand { get; }

		// Token: 0x0600017C RID: 380 RVA: 0x0004360C File Offset: 0x0004180C
		public OnLineTaskViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
		{
			this.<List>k__BackingField = new List<OlTask>();
			base..ctor();
			this.NavigationService = navigationService;
			this.PageDialogService = pageDialogService;
			this.OKCommand = new DelegateCommand(new Action(this.OnOKCommandExecuted));
			this.DingCommand = new DelegateCommand<OlTask>(new Action<OlTask>(this.OnDingCommandExecuted));
			this.TreadCommand = new DelegateCommand<OlTask>(new Action<OlTask>(this.OnTreadCommandExecuted));
			this.DelCommand = new DelegateCommand<OlTask>(new Action<OlTask>(this.OnDelCommandExecuted));
			LiteCollection<TaskComment> collection = Runtime.ConfigDatabase.GetCollection<TaskComment>();
			this.Operation = collection.FindAll().ToList<TaskComment>();
		}

		// Token: 0x0600017D RID: 381 RVA: 0x0004067F File Offset: 0x0003E87F
		public void OnNavigatedFrom(INavigationParameters parameters)
		{
		}

		// Token: 0x0600017E RID: 382 RVA: 0x000436BC File Offset: 0x000418BC
		public async void OnNavigatedTo(INavigationParameters parameters)
		{
			await this.GetOLTaskList();
			this.GetList();
		}

		// Token: 0x0600017F RID: 383 RVA: 0x0004067F File Offset: 0x0003E87F
		public void OnNavigatingTo(INavigationParameters parameters)
		{
		}

		// Token: 0x06000180 RID: 384 RVA: 0x000436F8 File Offset: 0x000418F8
		private async Task GetOLTaskList()
		{
			this.sourceList.Clear();
			foreach (Row row in JsonConvert.DeserializeObject<RootDataobject>(await Runtime.Remote.GetOlTaskList()).rows)
			{
				OlTask olTask = new OlTask();
				olTask._id = row.id;
				object obj = row.value[0];
				olTask.name = ((obj != null) ? obj.ToString() : null);
				olTask.author = row.value[1].ToString();
				olTask.memo = row.value[2].ToString();
				olTask.time = Convert.ToDateTime(row.value[3]);
				olTask.platform = row.value[4].ToString();
				object obj2 = row.value[5];
				olTask.node = ((obj2 != null) ? obj2.ToString() : null);
				olTask.classify = Convert.ToInt32(row.value[6]);
				olTask.ding = Convert.ToInt32(row.value[7]);
				olTask.tread = Convert.ToInt32(row.value[8]);
				this.sourceList.Add(olTask);
			}
		}

		// Token: 0x06000181 RID: 385 RVA: 0x00043740 File Offset: 0x00041940
		public void GetList()
		{
			List<OlTask> list;
			if (this.Type != 0)
			{
				list = (from i in this.sourceList
				where i.classify == this.Type - 1
				orderby i.time descending
				select i).ToList<OlTask>();
			}
			else
			{
				list = (from i in this.sourceList
				orderby i.time descending
				select i).ToList<OlTask>();
			}
			this.List = list;
		}

		// Token: 0x06000182 RID: 386 RVA: 0x000437CC File Offset: 0x000419CC
		private async void OnOKCommandExecuted()
		{
			if (this.Select != null)
			{
				ObservableCollection<AttackTask> observableCollection = this.Game.AttackTasks;
				AttackTask item = await Runtime.Remote.GetOlTaskData(this.Select._id);
				observableCollection.Add(item);
				observableCollection = null;
				await this.NavigationService.GoBackAsync();
			}
		}

		// Token: 0x06000183 RID: 387 RVA: 0x00043808 File Offset: 0x00041A08
		private async void OnDingCommandExecuted(OlTask task)
		{
			if (this.Operation.All((TaskComment i) => i.SourceID != task._id))
			{
				task.ding++;
				await Runtime.Remote.UpdateTask(task);
				LiteCollection<TaskComment> collection = Runtime.ConfigDatabase.GetCollection<TaskComment>();
				TaskComment taskComment = new TaskComment
				{
					SourceID = task._id,
					Operation = true
				};
				this.Operation.Add(taskComment);
				collection.Upsert(taskComment);
			}
		}

		// Token: 0x06000184 RID: 388 RVA: 0x0004384C File Offset: 0x00041A4C
		private async void OnTreadCommandExecuted(OlTask task)
		{
			if (this.Operation.All((TaskComment i) => i.SourceID != task._id))
			{
				task.tread++;
				await Runtime.Remote.UpdateTask(task);
				LiteCollection<TaskComment> collection = Runtime.ConfigDatabase.GetCollection<TaskComment>();
				TaskComment taskComment = new TaskComment
				{
					SourceID = task._id,
					Operation = true
				};
				this.Operation.Add(taskComment);
				collection.Upsert(taskComment);
			}
		}

		// Token: 0x06000185 RID: 389 RVA: 0x00043890 File Offset: 0x00041A90
		private async void OnDelCommandExecuted(OlTask task)
		{
			TaskAwaiter<bool> taskAwaiter = this.PageDialogService.DisplayAlertAsync("AutoR", "是否删除？", "确定", "取消").GetAwaiter();
			if (!taskAwaiter.IsCompleted)
			{
				await taskAwaiter;
				TaskAwaiter<bool> taskAwaiter2;
				taskAwaiter = taskAwaiter2;
				taskAwaiter2 = default(TaskAwaiter<bool>);
			}
			if (taskAwaiter.GetResult())
			{
				await Runtime.Remote.DelTask(task);
				await this.GetOLTaskList();
				this.GetList();
			}
		}

		// Token: 0x0400011B RID: 283
		private List<OlTask> sourceList = new List<OlTask>();
	}
}
